package com.woniuxy.reader.service.impl;

import cn.hutool.core.util.RandomUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.woniuxy.reader.entity.Author;
import com.woniuxy.reader.entity.Book;
import com.woniuxy.reader.entity.BookData;
import com.woniuxy.reader.entity.Chapter;
import com.woniuxy.reader.entity.Comment;
import com.woniuxy.reader.mapper.*;
import com.woniuxy.reader.mapper.AuthorMapper;
import com.woniuxy.reader.mapper.BookMapper;
import com.woniuxy.reader.service.BookService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.reader.vo.BookVO;
import com.woniuxy.reader.vo.BookVOOO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zh_o
 * @since 2020-10-12
 */
@Service
public class BookServiceImpl extends ServiceImpl<BookMapper, Book> implements BookService {

    @Resource
    private BookMapper bookMapper;

    @Resource
    private AuthorMapper authorMapper;

    @Resource
    private CategoryMapper categoryMapper;

    @Resource
    private ChapterMapper chapterMapper;

    @Resource
    private CommentMapper commentMapper;

    @Resource
    private BookDataMapper bookDataMapper;

    @Override
    public List<Book> selectAll() {
        List<Book> list = bookMapper.selectList(null);
        return list;
    }

    @Override
    public List<Book> selectData(int id) {
        return bookMapper.selectData(id);
    }

    @Override
    public List<Book> selectNoSign() {
        return bookMapper.selectNoSign();
    }

    @Override
    public void updateBook(int bookid) {
        bookMapper.updateBook(bookid);
    }

    @Override
    public void updateBookFlag(int bookid) {
        bookMapper.updateBookFlag(bookid);
    }

    @Override
    public List<BookVOOO> selectAllBook() {
        return bookMapper.selectAllBookDetail();
    }

    @Override
    public void setBoutique(int bookid) {
        bookMapper.setBoutique(bookid);
    }

    @Override
    public void setnormal(int bookid) {
        bookMapper.setnormal(bookid);
    }


    /**
     　* @Author daxigua
     * @Description 通过分类id，查询新书
     * @Date 2020/10/15  16:00
     * @return 分类下的新书
     */
    @Override
    public List<Book> selectBooksByCreatetimeAndCategoryId(Integer id) {
        QueryWrapper<Book> wapper = new QueryWrapper<>();
        wapper.eq("category_id",id);
        wapper.last("group by create_time desc");
        return  bookMapper.selectList(wapper);
    }


    /**
     　* @Author daxigua
     * @Description  通过分类id 和 书的点击量 ，查询当前分类下的书的人气排行
     * @Date 2020/10/15  16:25
     * @Param  * @param id  分类id
     * @return 当前分类下的书的人气排行，降序排序
     */
    @Override
    public List<Book> selectBooksByCategoryIdAndClickNum(int id) {
        List<Book> books = bookMapper.selectBooksByCategoryIdAndClickNum(id);

        return books;
    }


    /**
     　* @Author daxigua
     * @Description  通过分类id 和 书的收藏量 ，查询当前分类下的书的本周强推
     * @Date 2020/10/15  16:53
     * @Param  * @param id  分类id
     * @return 当前分类下的书的本周强推，降序排序
     */
    @Override
    public List<Book> selectBooksByCategoryIdAndCollection(int id) {
        List<Book> books = bookMapper.selectBooksByCategoryIdAndCollection(id);

        return books;
    }

    /**
     　* @Author daxigua
     * @Description  通过分类id，和书籍状态查出当前分类下完结的书籍
     * @Date 2020/10/15  17:14
     * @Param  * @param null
     * @return 当前分类下完结的书籍
     */
    @Override
    public List<Book> selectBookByStauts(int id) {
        QueryWrapper<Book> wrapper = new QueryWrapper<>();
        wrapper.eq("category_id",id);
        wrapper.eq("status",2);
        return bookMapper.selectList(wrapper);
    }

    /**
     * 根据作者id查询书籍详情
     * @param authorid
     * @return
     */
    @Override
    public List<BookVO> managerselect(String authorid) {
        return bookMapper.managerselect(authorid);
    }


    @Override
    public List<Book> getBookByAid(Integer authorId) {
        //书籍表
        QueryWrapper<Book> queryWrapper= new QueryWrapper<Book>();
        queryWrapper.eq("author_id", authorId);
        List<Book> list=bookMapper.selectList(queryWrapper);
        return list;
    }

    /**
     * 根据书籍id 获取详细信息
     * @param bookId
     * @return
     */
    @Override
    public Book getBookDetailById(Integer bookId) {
        // 获取书籍
        Book book = bookMapper.selectById(bookId);
        // 获取其作者笔名
        book.setAuthorName(authorMapper.selectById(bookId).getPenName());
        // 获取分类
        book.setCategoryName(categoryMapper.selectById(book.getCategoryId()).getCategoryName());
        // 获取章节
        book.setChapterList(chapterMapper.selectList(new QueryWrapper<Chapter>().eq(Chapter.BOOK_ID, bookId).orderByAsc(Chapter.UPDATE_TIME)));
        // 获取评论
        book.setCommentList(commentMapper.selectList(new QueryWrapper<Comment>().eq(Comment.BOOK_ID, bookId).orderByDesc(Comment.COMMENT_DATE)));
        // 获取评论用户名yu与章节名
        for (Comment comment : book.getCommentList()) {
            // 全站匿名评论，随机用户名
            comment.setUserName(RandomUtil.randomString(6));
            // 获取批量章节名
            comment.setChapterName(chapterMapper.selectById(comment.getChapterId()).getChapterName());
        }
        // 获取书籍数据
        book.setBookData(bookDataMapper.selectOne(new QueryWrapper<BookData>().eq(BookData.BOOK_ID, bookId)));
        return book;
    }


    /**
     　* @Author daxigua
     * @Description 传入书名，进行模糊查询，查找所有包含改字符串的书
     * @Date 2020/10/16  14:55
     * @return
     */
    @Override
    public List<Book> selectBookLikeBookName(String bookname) {
        QueryWrapper<Book> wrapper = new QueryWrapper<>();
        wrapper.like("book_name",bookname);
        List<Book> books = bookMapper.selectList(wrapper);
        return books;
    }

    /**
     　* @Author daxigua
     * @Description  传入book实体，进行书籍新增
     * @Date 2020/10/17  12:11
     * @Param  * @param null
     * @return  0 新增失败，1新增成功
     */
    @Override
    public int insertBookByBook(Book book) {
        int insert = bookMapper.insert(book);

        return insert;
    }

}
